﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.Web.Controls;
using CNVP.Framework.Utils;
using CNVP.Framework;
using System.Text.RegularExpressions;

namespace CNVP.JShop.Web.Admin.Template
{
    public partial class Template : System.Web.UI.Page
    {
        public int rplistCount = 0;
        int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 54)) return;//判断页面权限
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            if (!IsPostBack)
            {
                var path = Request.QueryString["path"];
                if (path == null || path == "" || path == @"/" || !Regex.IsMatch(path, @"~/Template/Default/"))
                {
                    hfCurentPath.Value = @"~/Template/Default/";
                }
                else
                {
                    hfCurentPath.Value =path.ToString();
                }
                StartLoad(PageIndex);
                btDelete.Attributes.Add("onclick", "if(ifCheck()){return confirm('确定删除该记录吗?');}else{alert('请选择记录！');return false;};");
            }

        }

        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            int i, j;
            DataTable dtAll = FileUtils.GetAllFolderAndFile(hfCurentPath.Value);
            int pageNum = 11;
            DataTable dt = CNVP.Framework.Helper.DbHelper.GetPagedTable(dtAll, PageIndex, pageNum);

            j = (dtAll.Rows.Count / pageNum)+1;
            i = dtAll.Rows.Count;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    rplistCount = dt.Rows.Count;//行数
                    //----------------------------------------添加列------------------------------------------------
                    dt.Columns.Add("oFileName", typeof(string));
                    dt.Columns.Add("oFoldName", typeof(String));
                    dt.Columns.Add("oFileType", typeof(String));
                    dt.Columns.Add("oFileSize", typeof(string));
                    dt.Columns.Add("oFileModifyTime", typeof(string));
                    dt.Columns.Add("oPath", typeof(string));
                    dt.Columns.Add("oEditFilePath",typeof(string));

                    //----------------------------------------添加列结束--------------------------------------------
                    for (int k = 0; dt.Rows.Count > k; k++)
                    {
                        dt.Rows[k]["oFileName"] = dt.Rows[k]["FileName"].ToString();
                        dt.Rows[k]["oFoldName"] = dt.Rows[k]["FoldName"].ToString();
                        dt.Rows[k]["oFileType"] = dt.Rows[k]["FileType"].ToString();
                        dt.Rows[k]["oFileSize"] = dt.Rows[k]["FileSize"].ToString();
                        dt.Rows[k]["oFileModifyTime"] = dt.Rows[k]["FileModifyTime"].ToString();
                        dt.Rows[k]["oPath"] = hfCurentPath.Value;
                        if (dt.Rows[k]["FileType"].ToString() == "Folder")
                        {
                            dt.Rows[k]["oEditFilePath"] = @"FileManger.aspx?action=EditFolder&path=" + hfCurentPath.Value + @"&file=" + dt.Rows[k]["FileName"].ToString();
                        }
                        else
                        {
                            dt.Rows[k]["oEditFilePath"] = @"FileManger.aspx?action=EditFile&path=" + hfCurentPath.Value + @"&file=" + dt.Rows[k]["FileName"].ToString(); 
                        }
                    }
                }
            }
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }


        protected void btDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {
                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                if (cbCheck.Checked == true)
                {
                    string filePath = cbCheck.ToolTip.ToString();
                    if (FileUtils.FileExists(Server.MapPath(filePath)))
                    {
                        FileUtils.DeleteFile(Server.MapPath(filePath));
                    }
                    if (FileUtils.FolderExists(Server.MapPath(filePath)))
                    {
                        FileUtils.DeleteFolder(Server.MapPath(filePath));
                    }
                }
            }
            MessageUtils.ShowRedirect("删除成功。", Request.CurrentExecutionFilePath);
            StartLoad(1);
        }

        protected void ddlpageNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            StartLoad(1);
        }

        protected void btAddFolder_Click(object sender, EventArgs e)
        {
            Response.Redirect("FileManger.aspx?action=AddFolder&path=" + hfCurentPath.Value);
        }

        protected void btAddFile_Click(object sender, EventArgs e)
        {
            Response.Redirect("FileManger.aspx?action=AddFile&path=" + hfCurentPath.Value);
        }

        protected void btUpFolder_Click(object sender, EventArgs e)
        {
            if (hfCurentPath.Value != "" || hfCurentPath.Value != @"/")
            {
                string tempPath = hfCurentPath.Value.Substring(0, hfCurentPath.Value.LastIndexOf(@"/"));
                Response.Redirect("Template.aspx?path=" + tempPath.Substring(0, tempPath.LastIndexOf(@"/") + 1), true);
            }
        }
    }
}